<template>
	<view>
		<view class="success" v-if="flag">
			<view class="success_h1">
				{{ flag ==1 ? "获取胜利":"暂时失败"}}
			</view>
		
			<view class="success_box">
				<view class="success_box_head">
					<view class="t">
						排名
					</view>
					<view class="m">
						正确数/总体数
					</view>
					<view class="b">
						总分
					</view>
				</view>
				<view class="success_box_content" v-for="(item,index) in successData" :key="index">
					<view class="t">
						<u-avatar :src="item.images"></u-avatar>
						<text>{{item.name}}</text>

					</view>
					<view class="m">
						{{item.trueTotal}}/{{item.total}}
					</view>
					<view class="b">
						{{item.totalScore}}分
					</view>
				</view>
			</view>
			<view class="success_box_botton" @click="callback()">
				回顾本局答题 <u-icon name="arrow-right" color="#ffffff" size="16"></u-icon>
			</view>

		</view>
		<view class="success_end">
			<view class="u-demo-block__content" @click="godati()">
				<u-button text="继续答题" size="large" type="success" color="#D87093"> </u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: 'https://img-cdn-tc.dcloud.net.cn/uploads/avatar/000/09/75/97_avatar_max.jpg',
				newData: [],
				successData: [
				],
				infos:{},
				flag: null
			}
		},
		onLoad(params) {
			this.infos = JSON.parse(decodeURIComponent(params.data))
			this.init()
		},
		methods: {
			init() {
				uni.$u.http.get('/app/paper/success/' + this.infos.uuid).then((res) => {
					this.newData = res.data
					let data = this.classify(this.newData)

					console.log(data);
					data.forEach(item => {
						
						let trueTotal = 0
						item.data.forEach(j=>{
							if(j.flag==1)
							{
								trueTotal++
							}
						})
						if(this.infos.myuserInfo.userId == item.userId )
						{
							this.successData.push({
									"userId":item.userId,
									"name": this.infos.myuserInfo.username,
									"images": this.infos.myuserInfo.images,
									"total": item.data.length,
									"trueTotal": trueTotal,
									"totalScore": trueTotal*5,
							})
						}
						if(this.infos.otherInfo.userId == item.userId )
						{
							this.successData.push({
									"userId":item.userId,
									"name": this.infos.otherInfo.username,
									"images": this.infos.otherInfo.images,
									"total": item.data.length,
									"trueTotal": trueTotal,
									"totalScore": trueTotal*5,
							})
						}
						
					})
					
					
					this.successData.sort(function(a,b){
						return b.totalScore -a.totalScore
					})
			
					if(this.successData[0].userId == this.infos.myuserInfo.userId)
					{
						this.flag =1
					}
				}).catch(() => {

				})
			},
			callback() {
				uni.showToast({
					title: '忙不过来了',
					icon: 'none'
				});
			},
			godati()
			{
				uni.navigateTo({
					url:'../ceshi/ceshi'
				})
			},
			classify(arr) {
				let map = {};
				let myArr = [];
				let num = 0
				for (let i = 0; i < arr.length; i++) {
					if (!map[arr[i].userId]) {
						num++
						myArr.push({
							userId: arr[i].userId,
							data: [arr[i]]
						});
						map[arr[i].userId] = arr[i]

					} else {

						for (let j = 0; j < myArr.length; j++) {
							if (arr[i].userId === myArr[j].userId) {
								myArr[j].data.push(arr[i]);
								break
							}
						}
					}
				}
				return myArr
			},
		}
	}
</script>

<style lang="scss" scoped>
	.success {
		height: 500rpx;
		background-color: skyblue;
		border-radius: 5rpx;
		margin: 40rpx 30rpx 0;
		display: flex;
		flex-direction: column;

		.success_h1 {
			margin: 20rpx;
			height: 30%;
			text-align: center;
			line-height: 100rpx;
			font-size: 50rpx;
			font-weight: 400;
			color: #FFFFFF;
			border-bottom: 1px solid #CCCCCC;
		}

		.success_box {
			height: 50%;

			.success_box_head {
				display: flex;
				flex-direction: row;
				color: #FFFFFF;
				justify-content: space-around;
				align-items: center;

			}

			.success_box_content {
				display: flex;
				flex-direction: row;
				color: #FFFFFF;
				justify-content: space-around;
				align-items: center;
				padding: 10rpx 0;

				.t {
					display: flex;
					flex-direction: row;
					align-items: center;

				}
			}

		}

		.success_box_botton {
			height: 10%;
			display: flex;
			font-size: 16px;
			line-height: 25px;
			font-weight: 400;
			color: #FFFFFF;
			justify-content: center;
			align-content: center;

		}


	}

	.success_end {
		padding: 30px;
	}

	.u-demo-block__content {

		flex-direction: column;
		align-items: stretch;
		flex-wrap: nowrap;
	}
</style>
